const dbCongif=require('../util/dbconfig')
//判断用户是否存在
goods_user=(req,res)=>{
    var name=req.body.name
    var sql="select user_id from sp_user where username=?"
    var sqlArr=[name];
    var callBack=(err,data)=>{
        if(err){
          console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
//注册
goods_zuce=(req,res)=>{
    var username=req.body.username
    var password=req.body.password
    var user_tel=req.body.user_tel
    var type=req.body.type
    var sql=`insert into sp_user (username,password,user_tel,type) values('${username}','${password}',${user_tel},'${type}')`
    var sqlArr=[];
    var callBack=(err,data)=>{
        if(err){
          console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
//登录
goods_login=(req,res)=>{
    var username=req.body.username
    var sql=`select * from sp_user where user_tel=?`
    var sqlArr=[username];
    var callBack=(err,data)=>{
        if(err){
          console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 获取我的收货地址
user_locations=(req,res)=>{
    var user_id=req.query.user_id
    var sql="select * from sp_consignee where user_id=? and cgn_default!='true'  order by cgn_id desc"
    var sqlArr=[user_id]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 获取我的默认收货地址
user_locations_default=(req,res)=>{
    var user_id=req.query.user_id
    var sql="select * from sp_consignee where user_id=? and cgn_default='true'"
    var sqlArr=[user_id]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 添加我的收货地址
user_address=(req,res)=>{
    var user_id=req.query.user_id
    var cgn_name=req.query.cgn_name
    var cgn_tel=req.query.cgn_tel
    var cgn_address=req.query.cgn_addres
    var cgn_default=req.query.cgn_default
    var sql=`insert into sp_consignee (user_id,cgn_name,cgn_tel,cgn_address,cgn_default) values(${user_id},'${cgn_name}','${cgn_tel}','${cgn_address}','${cgn_default}')`
    var sqlArr=[]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 编辑我的收货地址
user_editaddress=(req,res)=>{
    var cgn_id=req.query.cgn_id
    var sql="select * from sp_consignee where cgn_id=?"
    var sqlArr=[cgn_id]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 更新我的收货地址
user_updateaddress=(req,res)=>{
    var cgn_id=req.query.cgn_id
    var cgn_name=req.query.cgn_name
    var cgn_tel=req.query.cgn_tel
    var cgn_address=req.query.cgn_addres
    var cgn_default=req.query.cgn_default
    var sql=`update sp_consignee set cgn_name=?,cgn_tel=?,cgn_address=?,cgn_default=? where cgn_id=${cgn_id}`
    var sqlArr=[cgn_name,cgn_tel,cgn_address,cgn_default]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 更新默认收货地址
user_updatedefault=(req,res)=>{
    var cgn_id=req.query.cgn_id
    var sql=`update sp_consignee set cgn_default='false' where cgn_id=${cgn_id}`
    var sqlArr=[]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}
// 删除收货地址
user_delete=(req,res)=>{
    var cgn_id=req.query.cgn_id
    var sql=`delete from sp_consignee  where cgn_id=?`
    var sqlArr=[cgn_id]
    var callBack=(err,data)=>{
        if(err){
            console.log("连接失败了")
        }else{
            res.send({
                "data":data
            })
        }
    }
    dbCongif.sqlConnect(sql,sqlArr,callBack)
}

module.exports={
    goods_user,
    goods_zuce,
    goods_login,
    user_locations,
    user_address,
    user_editaddress,
    user_locations_default,
    user_updateaddress,
    user_updatedefault,
    user_delete
}